
-- Description: 游戏主逻辑

local skynet = require("skynet")
local Timer = require("Timer")
local FSM = require("FSM")


--全局变量本地化
local math_floor = math.floor
local math_random = math.random
local math_abs = math.abs
local table_insert = table.insert
local table_remove = table.remove

local DeskBase = import("DeskBase")
local Desk = class("Desk",DeskBase)

--构造函数
function Desk:ctor()
	self.super:ctor()

end

----------------------------------------------------------------------------------------------
---------游戏
---------------------------------------------------------------------------------------------- 
function Desk:start()
	dbglog("___救济金活动_开始了__")
	self.super.start(self) --基础的对象里在里面有初始化的了

	--数据初始化
	self:init()
	--状态时间
	self.state_time = {	
	
	}
	--游戏状态
	--state1 --eventName --state2 --callback
	self.state_list = {			
		{"rest",			"to_start",			"start", 		function() self:onStart() end },
		{"over",			"to_rest", 			"rest", 		function() self:onRest() end },
		{"start",			"to_over", 			"over", 		function() self:onOver() end },
	}
	self.fsm = FSM.new(self.state_list) --状态机
	--启动状态机
	self.fsm:set("over")
	self:changeState("to_rest")	
end

function Desk:init()
    self.open_time_list = {} --触发时间段
    self.act_data = self.config:getActivity()
    self.ac_content = self.act_data.ac_content
 

end

--一局结束后重置数据
function Desk:reset()

    self.config:reload()
    self.act_data = self.config:getActivity()
    self.ac_content = self.act_data.ac_content
   
    self.user_manager:clear() --清掉玩家

end

------------------------------------------------------------------
--state
------------------------------------------------------------------
function Desk:onRest()

    self:reset()  

    local function callback()
    	self:changeState("to_start")
		return false 	
    end
    self:setTimer(1, callback)
end

function Desk:onStart()

	-- --15点检查redis里是否有要发奖的玩家
	-- local today = os.date("*t")
	-- local time = os.time()
	-- if today.hours == 15 and today.min == 0 then 
	-- 	local user_list = self:callRedis(nil, "hgetall", "rescue_act")
	-- 	for key, v in pairs(user_list) do 
	-- 		local data = cjson.decode(v) 
	-- 		if not is_same_day(data.time, time) then 
	-- 			--不是同一天,发奖励
	-- 			local coins = data.coins
	-- 			local uid = data.uid
	-- 		    local user = self.user_manager:addUser(uid) 
	-- 		    if not user then 
	-- 		        errlog("_____用户不存在__",uid)
	-- 		        return 
	-- 		    end				
	-- 		    self:addChip(user, coins, reason.RESCUE)
	-- 		    --发送邮件
	-- 		    local mail = {}
	-- 		    mail.uid = uid
	-- 		    mail.title = string.format(mail_def.RESCUE_TITLE)
	-- 		    mail.content = string.format(mail_def.RESCUE_CONTENT, math_floor(coins/100))
	-- 		    mail.coins = 0
	-- 		    mail.mail_type = 2
	-- 		    mail.op_user = mail_def.OP_USER	
	-- 		    local mail_list = {mail}
	-- 		    self.desk:sendAwardMail(mail_list)
	-- 		    self:sendRedis(uid, "del", "rescue_act", key)
	-- 		end
	-- 	end
	-- end
    local function callback()
    	self:changeState("to_over")
		return false 	
    end
    self:setTimer(1, callback)

end

--所有时间段结束了
function Desk:onOver()
		
    local function callback()
    	self:changeState("to_rest")
		return false 	
    end
    self:setTimer(1, callback)
end

return Desk